/**
 * Created by Panda on 16/01/2017
 */
(function () {
    'use strict';
    //var topNavs = [{
    //    key: "0",
    //    text: "校内现有系统",
    //    links: [{
    //        key: "0",
    //        text: "系统1",
    //        des:"我是描述",
    //        url:"http://www.baidu.com"
    //    }, {
    //        key: "1",
    //        text: "系统2",
    //        des:"我是描述",
    //        url:""
    //    }, {
    //        key: "2",
    //        text: "系统3",
    //        des:"我是描述",
    //        url:""
    //    }]
    //}, {
    //    key: "1",
    //    text: "校外现有系统"
    //}];

    angular
        .module('com.akxx.aqzyk')
        .controller('SysconnectionController', SysconnectionController);


    /** @ngInject */
    function SysconnectionController($state,SweetAlert,_Sysconnection) {
        var vm = this;
        vm.newNav =false;
        vm.newLink =false;
        vm.overlay =false;
        vm.isReadonly = false;
        vm.topNavItem = {};
        vm.linkItem = {};
        getTopNavList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        function getTopNavList(param) {
            _Sysconnection.SysCon.get(param).then(function (res) {
                vm.topNavs = res.data;
                vm.active = vm.topNavs[0].id;
                var searchCode = vm.topNavs[0].code;
                getLinkList({async: true,sortExpr:"name desc","q[fld op]":searchCode});
            });
        };

        function getLinkList(param) {
            _Sysconnection.SysLink.get(param).then(function (res) {
                if(res.success){
                    vm.links = res.data;
                }else{
                    SweetAlert.swal("连接失败");
                }
            })
        }

        vm.topNavClickEvent = function (id) {
            vm.active = id;
            for(var i=0;i<vm.topNavs.length;i++){
                if(vm.topNavs[i].id == id){
                    var searchCode = vm.topNavs[i].code;
                    console.log(searchCode);
                    getLinkList({async: true,sortExpr:"name desc","q[fld op]":searchCode});
                }
            }
        };

        vm.closeNav = function (id) {
            var param = {id:id};
            _Sysconnection.DelSysCon.get(param).then(function (res) {
                if(res.success){
                    SweetAlert.swal("删除成功");
                }
            });
        };
        //新建tab事件
        vm.addNavEvent = function(){
            vm.newNav = true;
            vm.overlay = true;
            vm.saveNavType = "new";
        };
        // 编辑tab事件
        vm.editNavEvent = function (id) {
            console.log(id)
            var param={id:id}
            _Sysconnection.VisitSysCon.get(param).then(function (res) {
                if(res.success){
                    vm.topNavItem = res.data;
                    vm.newNav = true;
                    vm.overlay = true;
                    vm.saveNavType = "edit";
                    vm.saveNavId = id;
                }
            });
        }

        //关闭弹窗
        vm.closeFrame = function(){
            vm.newNav = false;
            vm.newLink = false;
            vm.overlay = false;
            vm.isReadonly = false;
        };

        // 保存
        vm.saveTopNav = function () {
            if(vm.saveNavType = "new"){
                var param = vm.topNavItem;
                _Sysconnection.AddSysLink.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("新建成功");
                        vm.newNav =false;
                    }
                });
            }else if(vm.saveNavType = "edit"){
                var param = vm.topNavItem;
                param.id = vm.saveNavId;
                _Sysconnection.EditSysCon.get(param).then(function (res) {
                    if (res.success) {
                        SweetAlert.swal("编辑成功")
                        vm.newNav =false;
                    }
                });
            }
            getTopNavList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }

        // 链接保存
        vm.saveLink = function () {
            if(vm.saveLinkType = "new"){
                var param = vm.linkItem;
                _Sysconnection.AddSysCon.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("新建成功");
                        vm.newLink =false;
                    }
                });
            }else if(vm.saveLinkType = "edit"){
                var param = vm.linkItem;
                param.id = vm.saveLinkId;
                _Sysconnection.EditSysLink.get(param).then(function (res) {
                    if (res.success) {
                        SweetAlert.swal("编辑成功")
                        vm.newLink =false;
                    }
                });
            }
            getTopNavList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }

        //链接划过事件
        vm.linkItemShow=function(id){
            vm.isShow = id;
        }
        vm.linkItemHide=function(id){
            vm.isShow = "";
        }

        //新建链接事件
        vm.linkItemAdd = function(id){
            vm.newLink = true;
            vm.overlay = true;
            vm.saveLinkType = "new";
            vm.saveLinkId = id;
        }
        // 编辑链接事件
        vm.linkItemEdit = function (id) {
            var param={id:id}
            _Sysconnection.VisitSysLink.get(param).then(function (res) {
                if(res.success){
                    vm.linkItem = res.data;
                    vm.newLink = true;
                    vm.overlay = true;
                    vm.saveLinkType = "edit";
                    vm.saveLinkId = id;
                }
            });
        }
        // 查看链接事件
        vm.linkItemVisit = function (id) {
            var param={id:id}
            _Sysconnection.VisitSysLink.get(param).then(function (res) {
                if(res.success){
                    vm.linkItem = res.data;
                    vm.newLink = true;
                    vm.overlay = true;
                    vm.isReadonly = true;
                }
            });
        }

        //删除链接事件
        vm.linkItemDel = function(id){
            var delArr = vm.links[id]
            vm.links.splice($.inArray(delArr, vm.links), 1);
            getTopNavList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }
    }
})();

